package ${webPackageName}.controller.other;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.redisson.api.RLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import ${commonPackageName}.annotation.RequestLog;
import ${commonPackageName}.strategy.RedissonLocker;
import ${commonPackageName}.utils.ExcelClient;
import ${servicePackageName}.entity.sys.SysRole;
import com.ejlchina.okhttps.FastjsonMsgConvertor;
import com.ejlchina.okhttps.HTTP;

import cn.hutool.core.lang.Pair;
import cn.hutool.core.map.MapUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 例子
 * @author lleice
 *
 */
@Api(tags = {"例子"})
@RestController
@RequestMapping("/base/example")
<#noparse>
public class ExampleController {
	@Autowired
	private RedissonLocker redissonLocker;

	@ApiOperation(value = "分布式上锁(可以设置等待锁的时间)")
	@RequestLog
    @RequestMapping(value = "/redissonTryLock", method = RequestMethod.GET)
	public void redissonTryLock(@RequestParam("key") String key,@RequestParam("waittime") Integer waittime,@RequestParam("leaseTime") Integer leaseTime) throws Exception{
		boolean lock =  redissonLocker.tryLock(key, TimeUnit.SECONDS, waittime, leaseTime);
		System.out.println(lock?"上锁成功":"上锁失败");
    }
	
	@ApiOperation(value = "分布式上锁(等待锁时一直block)")
	@RequestLog
    @RequestMapping(value = "/redissonLock", method = RequestMethod.GET)
	public void redissonLock(@RequestParam("key") String key,@RequestParam("leaseTime") Integer leaseTime) throws Exception{
		RLock lock =  redissonLocker.lock(key, leaseTime);
		System.out.println(lock.isLocked()?"上锁成功":"上锁失败");
    }
	
	@ApiOperation(value = "分布式解锁(需要同一个线程内)")
	@RequestLog
    @RequestMapping(value = "/redissonUnLock", method = RequestMethod.GET)
	public void redissonUnLock(@RequestParam("key") String key) throws Exception{
		redissonLocker.unlock(key);
		System.out.println("解锁锁成功");
    }
	
	@ApiOperation(value = "分布式解锁(不需要同一个线程内)")
	@RequestLog
    @RequestMapping(value = "/redissonForceUnLock", method = RequestMethod.GET)
	public void redissonForceUnLock(@RequestParam("key") String key) throws Exception{
		redissonLocker.forceUnlock(key);
		System.out.println("解锁锁成功");
    }

	@SuppressWarnings("unchecked")
	@ApiOperation(value = "导入excel")
	@RequestLog
    @RequestMapping(value = "/input", method = RequestMethod.POST)
	public void test(@RequestParam("file") MultipartFile uploadFile) throws Exception{
    	List<SysRole> list = ExcelClient.excelToList(uploadFile, 
    			MapUtil.of(new Pair[] {
    					new Pair<String, String>("角色名称", "roleName"),
    					new Pair<String, String>("角色编码", "roleCode")}), 
    			SysRole.class);
    	list.forEach(a ->{
    		System.out.println(a.getRoleCode()+","+a.getRoleName());
    	});
    }
	
	/**
	 * 代替原生httpClient  
	 * 文档地址:http://okhttps.ejlchina.com/
	 * @throws Exception
	 */
	@ApiOperation(value = "httpClient")
	@RequestLog
    @RequestMapping(value = "/httpClient", method = RequestMethod.POST)
	public void httpClient() throws Exception{
		HTTP http = HTTP.builder().
				baseUrl("http://10.91.26.2:8899").
				bodyType("json").
				addMsgConvertor(new FastjsonMsgConvertor()).
				build();
		
		String result  = http.sync("/ruleBase/typeCatalog")
		.addHeader("Authorization", "49d09894-6b24-4c51-89a4-0630d6e9f6e5")
        .post()                
        .getBody().toString();
		JSONObject json = (JSONObject) JSONObject.parse(result);
		JSONObject jsonResult = json.getJSONObject("result");
		JSONArray jsonArray = json.getJSONArray("data");
		
		System.out.println(jsonResult.getInteger("result_code")+","+jsonResult.getString("result_desc"));
		System.out.println(jsonArray.toJSONString());
    }
	
}
</#noparse>